package com.itheima.askme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.askme.entity.Question;
import com.itheima.askme.mapper.QuestionMapper;
import com.itheima.askme.query.QuestionQuery;
import com.itheima.askme.service.IQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.askme.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dafei
 * @since 2022-06-14
 */
@Service
@Transactional
@Slf4j
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

    @Autowired
    private IUserService iUserService;
    @Override
    public Page<Question> queryPage(QuestionQuery query) {
        Page<Question> page=new Page<>(query.getCurrentPage(),query.getPageSize());
        LambdaQueryWrapper<Question> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Question::getStatus,query.getStatus());
        wrapper.orderByDesc(Question::getScore);
//        QueryWrapper<Question> wrapper = new QueryWrapper<>();
//        wrapper.eq(query.getStatus() > -1, "status", query.getStatus());
//
//        wrapper.orderByDesc(StringUtils.hasText(query.getOrderBy()), query.getOrderBy());

        Page<Question> pagelist = page(page, wrapper);
        for (Question record: page.getRecords()){
           record.setAsk(iUserService.getById(record.getAskId()));
        }
        return pagelist;
    }
}
